import 'package:core_ui/base/base_page.dart';
import 'package:core_ui/base/refresh/base_refresh_logic.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

// ignore: must_be_immutable
abstract class BaseRefreshPage<B extends BaseRefreshLogic> extends BasePage<B> {
  late ListView _listView;

  BaseRefreshPage({Key? key}) : super(key: key);

  @override
  Widget? onCreateSuccess(BuildContext context) {
    return Obx(() {
      _listView = ListView.builder(
        itemBuilder: onItemBuilder,
        itemCount: logic.data.length,
      );

      return SmartRefresher(
        controller: logic.refreshController,
        enablePullDown: logic.enableRefresh.value,
        enablePullUp: logic.enableLoadMore.value,
        header: onCreateRefreshView(),
        footer: onCreateLoadMoreView(),
        onRefresh: () => logic.onRefreshOrLoadMore(true),
        onLoading: () => logic.onRefreshOrLoadMore(false),
        child: _listView,
      );
    });
  }

  Widget? onCreateRefreshView() => null;

  Widget? onCreateLoadMoreView() => null;

  Widget onItemBuilder(BuildContext context, int index);
}
